<?php

if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

class Group_MxShopPage extends WebPage {

    function main() {
        global $_W, $_GPC;
        $list = array(
            array(
                'id' => 'default',
                'groupname' => '无分组',
                'usercount' => pdo_fetchcolumn('select count(*) from ' . tablename('mx_shop_user') . ' where groupid=0 limit 1')
            )
        );

        $condition = " and uniacid=:uniacid";
        $params = array(':uniacid' => $_W['platformid']);

        if (!empty($_GPC['keyword'])) {
            $_GPC['keyword'] = trim($_GPC['keyword']);
            $condition.=' and ( groupname like :groupname)';
            $params[':groupname'] = "%{$_GPC['keyword']}%";
        }

        $alllist = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_user_group') . " WHERE 1 {$condition} ORDER BY id asc", $params);
        foreach ($alllist as &$row) {
            $row['usercount'] = pdo_fetchcolumn('select count(*) from ' . tablename('mx_shop_user') . ' where groupid=:groupid limit 1', array(':groupid' => $row['id']));
        }
        unset($row);

        if(empty($_GPC['keyword'])){
            $list = array_merge($list, $alllist);
        }else{
            $list = $alllist;
        }

        include $this->template();
    }

    function add() {
        $this->post();
    }

    function edit() {
        $this->post();
    }

    protected function post() {
        global $_W, $_GPC;
        $id = intval($_GPC['id']);
        $group = pdo_fetch("SELECT * FROM " . tablename('mx_shop_user_group') . " WHERE id =:id and uniacid=:uniacid limit 1", array(':id' => $id, ':uniacid' => $_W['platformid']));
        if ($_W['ispost']) {

            $data = array(
                'uniacid' => $_W['platformid'],
                'groupname' => trim($_GPC['groupname']),
            );

            if (!empty($id)) {
                pdo_update('mx_shop_user_group', $data, array('id' => $id, 'uniacid' => $_W['platformid']));
                plog('user.group.edit', "修改会员分组 ID: {$id}");
            } else {
                $data['createtime'] = time();
                pdo_insert('mx_shop_user_group', $data);
                $id = pdo_insertid();
                plog('user.group.add', "添加会员分组 ID: {$id}");
            }
            show_json(1, array('url' => webUrl('user/group', array('op' => 'display'))));
        }
        include $this->template();
    }

    function delete() {
        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }
        
        $items = pdo_fetchall("SELECT id,groupname FROM " . tablename('mx_shop_user_group') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);
        foreach ($items as $item) {
        	pdo_update('mx_shop_user', array('groupid'=>0), array('groupid' => $item['id']));
            pdo_delete('mx_shop_user_group', array('id' => $item['id']));
            plog('user.group.delete', "删除分组 ID: {$item['id']} 名称: {$item['groupname']} ");
        }
        show_json(1, array('url' => referer()));
    }

}
